//
//  ViewReusePool.m
//  Charles
//
//  Created by Charles on 2019/3/13.
//  Copyright © 2019 charles. All rights reserved.
//

#import "ViewReusePool.h"

@interface ViewReusePool ()
// 等待使用的队列
@property (nonatomic, strong) NSMutableSet *waitUsedQueue;
// 使用中的队列
@property (nonatomic, strong) NSMutableSet *usingQueue;

@end

@implementation ViewReusePool

- (instancetype)init {
    if (self = [super init]) {
        self.waitUsedQueue = [NSMutableSet set];
        self.usingQueue = [NSMutableSet set];
    }
    return self;
}

- (UIView *)dequeueReuseableView {
    UIView *view = [_waitUsedQueue anyObject];
    if (view == nil) {
        return nil;
    }else {
        // 进行队列移动
        [_waitUsedQueue removeObject:view];
        [_usingQueue addObject:view];
        return view;
    }
}

- (void)addUsingView:(UIView *)view {
    if (view == nil) {
        return;
    }
    // 添加视图到使用中队列
    [_usingQueue addObject:view];
}

-(void)reset {
    UIView *view = nil;
    while ((view = [_usingQueue anyObject])) {
        // 从使用中队列移除
        [_usingQueue removeObject:view];
        // 添加到等待使用队列
        [_waitUsedQueue addObject:view];
    }
}

@end
